package com.fas.system.controller;

import com.alibaba.fastjson.JSONArray;
import com.fas.common.annotation.Log;
import com.fas.common.core.controller.BaseController;
import com.fas.common.core.domain.AjaxResult;
import com.fas.common.core.domain.entity.SysDept;
import com.fas.common.core.domain.model.LoginUser;
import com.fas.common.core.page.TableDataInfo;
import com.fas.common.enums.BusinessType;
import com.fas.common.utils.SecurityUtils;
import com.fas.common.utils.poi.ExcelUtil;
import com.fas.system.domain.FasBuilding;
import com.fas.system.domain.FasBuildingImage;
import com.fas.system.service.IFasBuildingCategoryService;
import com.fas.system.service.IFasBuildingService;
import com.fas.system.service.ISysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 建筑公司Controller
 * 
 * @author ruoyi
 * @date 2022-04-27
 */
@Api(tags = "建筑公司")
@RestController
@RequestMapping("/system/building")
public class FasBuildingController extends BaseController
{
    @Autowired
    private IFasBuildingService fasBuildingService;

    @Autowired
    private IFasBuildingCategoryService fasBuildingCategoryService;
    @Resource
    private ISysDeptService deptService;

    /**
     * 查询总公司建筑公司列表
     */
    @ApiOperation(value = "查询总公司建筑公司列表")
//    @PreAuthorize("@ss.hasPermi('system:building:list')")
    @GetMapping("/list")
    public TableDataInfo list(FasBuilding fasBuilding,Integer pageNum,Integer pageSize){
        //封装数据为实体
        System.out.println("输入的参数为："+fasBuilding);
        startPage();
        List<FasBuilding> list = fasBuildingService.selectFasBuildingList(fasBuilding);
        return getDataTable(list);
    }
    /**
     * 查询维保单位建筑公司列表
     */
    @ApiOperation(value = "查询维保单位建筑公司列表")
//    @PreAuthorize("@ss.hasPermi('system:building:maintainList')")
    @GetMapping("/maintainList")
    public TableDataInfo maintainList(FasBuilding fasBuilding ,Integer pageNum,Integer pageSize)
    {
        System.out.println("输入的参数为："+fasBuilding);
        //封装数据为实体
        LoginUser loginUser = SecurityUtils.getLoginUser();
        fasBuilding.setDeptId(loginUser.getDeptId());
        startPage();
        List<FasBuilding> list = fasBuildingService.selectFasBuildingList(fasBuilding);
        return getDataTable(list);
    }

    /**
     * 查询维保单位建筑公司列表(不分页）
     */
    @ApiOperation(value = "查询维保单位建筑公司列表(不分页）")
//    @PreAuthorize("@ss.hasPermi('system:building:maintainList')")
    @GetMapping("/maintainListNoPage")
    public TableDataInfo maintainListNoPage(@RequestParam(value = "condition",required = false) String condition)
    {
        System.out.println("输入的参数为："+condition);
        //封装数据为实体
        LoginUser loginUser = SecurityUtils.getLoginUser();
        FasBuilding fasBuilding = new FasBuilding();
        fasBuilding.setDeptId(loginUser.getDeptId());
        fasBuilding.setCondition(condition);
//        startPage();
        List<FasBuilding> list = fasBuildingService.selectFasBuildingList(fasBuilding);
        return getDataTable(list);
    }

    /**
     * 小程序查询维保单位建筑公司列表
     */
    @ApiOperation(value = "查询维保单位建筑公司列表")
//    @PreAuthorize("@ss.hasPermi('system:building:xiaomaintainList')")
    @GetMapping("/xiaomaintainList")
    public TableDataInfo xiaomaintainList(FasBuilding fasBuilding )
    {
        System.out.println("输入的参数为："+fasBuilding);
        //封装数据为实体
        LoginUser loginUser = SecurityUtils.getLoginUser();
        fasBuilding.setDeptId(loginUser.getDeptId());
        startPage();
        List<FasBuilding> list = fasBuildingService.selectFasBuildingList(fasBuilding);
        return getDataTable(list);
    }


    /**
     * 导出建筑公司列表
     */
    @ApiOperation(value = "导出建筑公司列表")
//    @PreAuthorize("@ss.hasPermi('system:building:export')")
    @Log(title = "建筑公司", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody FasBuilding fasBuilding)
    {
        System.out.println("输入的参数："+ fasBuilding.toString());
        LoginUser user = SecurityUtils.getLoginUser();
        Long deptId = user.getDeptId();
        SysDept sysDept = deptService.selectDeptById(deptId);
        List<FasBuilding> list = new ArrayList<>();
        if("0".equals(sysDept.getDeptType())){
            list = fasBuildingService.selectFasBuildingList(fasBuilding);
        }else if("2".equals(sysDept.getDeptType())){
            fasBuilding.setDeptId(deptId);
            list = fasBuildingService.selectFasBuildingList(fasBuilding);
        }
        ExcelUtil<FasBuilding> util = new ExcelUtil<FasBuilding>(FasBuilding.class);
        util.exportExcel(response, list, "建筑公司数据");
    }

    /**
     * 批量导入建筑信息
     * @param file
     * @return
     */
    @PostMapping("/importData")
    @Log(title = "建筑公司", businessType = BusinessType.IMPORT)
    public AjaxResult importData(MultipartFile file) {
        try {
            ExcelUtil<FasBuilding> util = new ExcelUtil<>(FasBuilding.class);
            List<FasBuilding> buildingList = util.importExcel(file.getInputStream());
            buildingList.forEach(e -> e.setDeptId(getDeptId()));
            return fasBuildingService.importFasBuilding(buildingList);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 获取建筑公司详细信息
     */
    @ApiOperation(value = "获取建筑公司详细信息")
//    @PreAuthorize("@ss.hasPermi('system:building:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(fasBuildingService.selectFasBuildingById(id));
    }

    /**
     * 维保单位新增建筑公司
     */
    @ApiOperation(value = "新增建筑公司")
//    @PreAuthorize("@ss.hasPermi('system:building:add')")
    @Log(title = "建筑公司", businessType = BusinessType.INSERT)
    @PostMapping("/addBuliding")
    public AjaxResult add(@RequestBody FasBuilding fasBuilding)
    {
        //建筑名称、建筑ID不能重名
//        fasBuildingService.
        LoginUser loginUser = SecurityUtils.getLoginUser();
        fasBuilding.setDeptId(loginUser.getDeptId());
        System.out.println("登录维保id"+loginUser.getDeptId());
//        fasBuilding.setBuildingId(orderldUtil.getBulidingNo());

        return fasBuildingService.insertFasBuilding(fasBuilding);
    }

    /**
     * 修改建筑公司
     */
    @ApiOperation(value = "修改建筑公司")
//    @PreAuthorize("@ss.hasPermi('system:building:edit')")
    @Log(title = "建筑公司", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FasBuilding fasBuilding)
    {
        return toAjax(fasBuildingService.updateFasBuilding(fasBuilding));
    }

    /**
     * 删除建筑公司
     */
    @ApiOperation(value = "删除建筑公司")
//    @PreAuthorize("@ss.hasPermi('system:building:remove')")
    @Log(title = "建筑公司", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return fasBuildingService.deleteFasBuildingByIds(ids);
    }

    /**
     * 获取建筑楼层层数
     */
    @ApiOperation(value = "获取建筑楼层层数")
	@GetMapping("getBuildingFloor/{ids}")
    public TableDataInfo getBuildingFloor(@PathVariable Integer ids)
    {
        return getDataTable(fasBuildingService.getBuildingFloor(ids));
    }


    /**
     * 新增、修改设备时所属单位下建筑列表不为空的数据
     * @return
     */
    @ApiOperation(value = "新增、修改设备时所属单位下建筑列表不为空的数据")
//    @PreAuthorize("@ss.hasPermi('system:building:remove')")
    @GetMapping("/equipment_building")
    public AjaxResult equipmentBuilding(Integer buildingId,Integer facilitiesDistribution,Long deptId,Integer cooperationCompanyId,String buildingName){
        SysDept sysDept = deptService.selectDeptById(getDeptId());
        String deptType = sysDept.getDeptType();
        if (deptType.equals("1") || deptType.equals("2")){
            //单位不是总公司的查询本单位下的数据
            if(deptId == null){
                deptId = getDeptId();
            }
        }

        JSONArray jsonArray = fasBuildingService.findBuildingList(deptId, buildingId, facilitiesDistribution, cooperationCompanyId,buildingName);
        return AjaxResult.success(jsonArray);
    }

    /**
     * 查询建筑下的建筑图纸
     * @return
     */
    @ApiOperation(value = "查询建筑下的建筑图纸")
    @GetMapping("/findImageByBuildingId")
    public TableDataInfo findImageByBuildingId(Integer pageNum,Integer pageSize,Integer buildingId){
        startPage();
        return getDataTable(fasBuildingService.findImageByBuildingId(buildingId));
    }

    /**
     * 更新建筑图纸
     * @return
     */
    @ApiOperation(value = "更新建筑图纸")
    @Log(title = "单位建筑图纸", businessType = BusinessType.UPDATE)
    @PostMapping("/updateFasBuildingImage")
    public AjaxResult updateFasBuildingImage(@RequestBody FasBuildingImage fasBuildingImage){
        return toAjax(fasBuildingService.updateFasBuildingImage(fasBuildingImage));
    }

    /**
     * 查询合作单位下的建筑
     */
    @ApiOperation(value = "查询合作单位下的建筑")
    @GetMapping("/getDeptBuild")
    public TableDataInfo getDeptBuild(Integer cooperationCompanyId){
        Long deptId = SecurityUtils.getDeptId();
        return getDataTable(fasBuildingService.getDeptBuild(cooperationCompanyId,deptId.intValue()));
    }

    /**
     * 建筑管理-建筑类别
     */
    @ApiOperation(value = "建筑管理-建筑类别")
    @GetMapping("/dicCompanyCategory")
    public TableDataInfo dicCompanyCategory(){
        return getDataTable(fasBuildingCategoryService.dicCompanyCategory());
    }

}
